/*
 * GeneralPanel.java
 *
 * Created on February 13, 2008, 4:29 AM
 */

package gui.options;

import datamodel.WSFConfiguration;
import gui.WSFApplication;
import java.io.File;
import javax.swing.JDialog;
import org.apache.log4j.Logger;
import org.jdesktop.application.Action;

/**
 *
 * @author  chang
 */
public class GeneralPanel extends javax.swing.JPanel {
    
    private static Logger logger = Logger.getLogger(GeneralPanel.class);
    
    private boolean changed;
    
    
    /** Creates new form GeneralPanel */
    public GeneralPanel(JDialog optionsDialog) {
        initComponents();
        
        this.optionsDialog = optionsDialog;
        
        postInit();
    }
    
    private void postInit(){
        
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
        
        changed = false;
        
        WSFConfiguration config = WSFApplication.getApplication().getWSFConfiguration();
        projectsSavePathTextField.setText(config.getProjectsDirectory().getAbsolutePath());
    }
    
    public void saveChanges(){
        if(!changed)
            return;
        
        WSFConfiguration config = WSFApplication.getApplication().getWSFConfiguration();
        config.setProjectsDirectory(new File(projectsSavePathTextField.getText()));
        
        logger.info("Set Project Dir: " + projectsSavePathTextField.getText());
        
        changed = false;
    }
    
    
    @Action
    public void showFileChooserDialog(){
        
        if(fileChooser.showOpenDialog(jPanel1) == 0){
            projectsSavePathTextField.setText(fileChooser.getSelectedFile().getAbsolutePath());
            changed = true;
        }
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        projectsSavePathTextField = new javax.swing.JTextField();
        fileChooserButton = new javax.swing.JButton();

        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setName("fileChooser"); // NOI18N

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gui.WSFApplication.class).getContext().getResourceMap(GeneralPanel.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        projectsSavePathTextField.setText(resourceMap.getString("projectsSavePathTextField.text")); // NOI18N
        projectsSavePathTextField.setName("projectsSavePathTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gui.WSFApplication.class).getContext().getActionMap(GeneralPanel.class, this);
        fileChooserButton.setAction(actionMap.get("showFileChooserDialog")); // NOI18N
        fileChooserButton.setText(resourceMap.getString("fileChooserButton.text")); // NOI18N
        fileChooserButton.setName("fileChooserButton"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(projectsSavePathTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fileChooserButton)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectsSavePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fileChooserButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(123, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JButton fileChooserButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField projectsSavePathTextField;
    // End of variables declaration//GEN-END:variables
    
    private JDialog optionsDialog;
}
